#!/usr/bin/perl
use strict;

use String::Scanf;
use DBD::SQLite;
use Switch;
use Encode;
use Encode::Guess;
use Time::Local;
use POSIX qw(strftime);

use utf8;

Encode::Guess->set_suspects(/utf8 latin2/);

sub fix
{
	return $_[0];
}

my $dbh = DBI->connect("dbi:SQLite:History.db") || die "Cannot open database: $DBI::errstr";

sub putdata
{
	print "INSERT INTO history (timestamp, type, channel, author, target, text) VALUES("
	  . $_[0] . ", "
	  . $_[1]
	  . ", '#dragoart', "
	  . $_[2] . ",  "
	  . $_[3] . ", '"
	  . $_[4] . ")\n";
	$dbh->do(   "INSERT INTO history (timestamp, type, channel, author, target, text) VALUES("
		  . $dbh->quote($_[0]) . ", "
		  . $dbh->quote($_[1])
		  . ", '#dragoart', "
		  . $dbh->quote($_[2]) . ",  "
		  . $dbh->quote($_[3]) . ", "
		  . $dbh->quote($_[4])
		  . ")");
}

$dbh->do(
	"CREATE TABLE IF NOT EXISTS history (id INTEGER PRIMARY KEY, timestamp INTEGER, type, channel, author, target, text)"
);
$dbh->do("CREATE TABLE IF NOT EXISTS stats (channel, author, size INTEGER)");

if (@ARGV < 1) {
	print "You have to supply the logfile as an argument!\n";
}

open(LOGFILE, $ARGV[0]);

while (<LOGFILE>) {
	my $line = fix($_);
	(my $wday, my $mon, my $mday, my $year, my $hours, my $min, my $sec, my $msgident) =
	  sscanf("[%s %s %d %d] [%d:%d:%d] %s", $line);

	switch ($mon) {
		case "Jan" {$mon = 0;}
		case "sty" {$mon = 0;}
		case "Feb" {$mon = 1;}
		case "lut" {$mon = 1;}
		case "Mar" {$mon = 2;}
		case "mar" {$mon = 2;}
		case "Apr" {$mon = 3;}
		case "kwi" {$mon = 3;}
		case "May" {$mon = 4;}
		case "maj" {$mon = 4;}
		case "Jun" {$mon = 5;}
		case "cze" {$mon = 5;}
		case "Jul" {$mon = 6;}
		case "lip" {$mon = 6;}
		case "Aug" {$mon = 7;}
		case "sie" {$mon = 7;}
		case "Sep" {$mon = 8;}
		case "wrz" {$mon = 8;}
		case "Oct" {$mon = 9;}
		case "paz" {$mon = 9;}
		case "Nov" {$mon = 10;}
		case "lis" {$mon = 10;}
		case "Dec" {$mon = 11;}
		case "gru" {$mon = 11;}
	}
	my $timestamp;
	if ($year) {
		$timestamp = timelocal($sec, $min, $hours, $mday, $mon, $year);
	}

	switch ($msgident) {
		case '*' {
			$line =~ m/\*\s(.*)/;
			my $memsg = $1;

			my $idx = index($memsg, ' ');
			my $nick = substr($memsg, 0, $idx);
			my $msg = substr($memsg, $idx + 1);

			putdata($timestamp, 'me', $nick, '', $msg);
		}
		case "Topic" {
			if ($line =~ /Topic\s(.*)sets the channel/) {
				my $nick = $1;
				$line =~ /\"(.*)\"/;
				my $topic = $1;

				putdata($timestamp, 'topic', $nick, '', $topic);
			}
		}
		case /<*>/ {
			$line =~ m/<(.*?)>/;
			my $nick = $1;
			if (!($nick eq '-')) {
				$line =~ m/>\s(.*)/;
				my $message = $1;

				putdata($timestamp, 'public', $nick, '', $message);
			}
		}
		case "Join" {
			my $nick;
			my $host;
			if ($line =~ m/Join\s(.*) has/) {
				$nick = $1;
				$line =~ m/\((.*)\)/;
				$host = $1;
			}
			elsif ($line =~ m/Join\s(.*) have/) {
				$nick = "Rush";
				$line =~ m/\((.*)\)/;
				$host = $1;
			}
			putdata($timestamp, 'join', $nick, '', $host);
		}
		case "Quit" {
			$line =~ m/Quit\s(.*)/;
			my $idx = index($1, ' ');
			my $nick = substr($1, 0, $idx);
			$1 =~ m/\((.*)\)/;
			my $reason = $1;
			putdata($timestamp, 'quit', $nick, '', $reason);
		}
		case "Kick" {
			$line =~ m/Kick\s(.*)/;
			my $idx = index($1, ' ');
			my $nick = substr($1, 0, $idx);
			$line =~ m/by\s(.*)\s\(/;
			my $kicker = $1;
			$line =~ m/\((.*)\)/;
			my $reason = $1;

			putdata($timestamp, 'quit', $kicker, $nick, $reason);
		}
		case "Mode" {
			;    #IGNORE
		}
	}

	#	if(!$counter--) {
	#		exit;
	#	}
}
